<template>
  <div class="main">
    <div class="login box" v-if="logined">
      <div class="form" :class="loginedCss ? 'loginCss' : ''">
        <h2>登录</h2>
        <div class="link el-icon-edit" @click="toRegister"></div>
        <el-form :model="forms" key="login" :rules="formsRule" ref="forms">
          <el-form-item label="用户账号" prop="userName">
            <el-input v-model="forms.userName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="forms.password"></el-input>
          </el-form-item>
          <el-form-item style="margin-top: 50px">
            <el-button @click="loginFn('forms')">登录</el-button>
          </el-form-item>
          <!-- <el-form-item>
            <el-button @click="toRegister">register</el-button>
          </el-form-item> -->
        </el-form>
      </div>
    </div>
    <div class="register box" v-else>
      <div class="form" :class="!loginedCss ? 'registerCss' : ''">
        <h2>注册</h2>
         <div class="link el-icon-edit" @click="toLogin"></div>
        <el-form :model="form" :rules="formRule" ref="form" key="register">
          <el-form-item label="用户账号" prop="userName">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="surePassword">
            <el-input v-model="form.surePassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="registerFn('form')">注册</el-button>
          </el-form-item>
          <!-- <el-form-item>
            <el-button @click="tologin">login</el-button>
        </el-form-item> -->
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      logined: true,
      loginedCss: null,

      forms: {},
      form: {
        userName: "",
        password: "",
        surePassword: "",
      },
      formRule: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 6, message: "请输入4-6个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        surePassword: [{ validator: validatePass2, trigger: "blur" }],
      },
      formsRule: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 6, message: "请输入4-6个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    loginFn(formName) {
            this.$refs[formName].validate((valid) => {
        if (valid) {
             this.$http.post("/user/login", this.forms).then((res) => {
            if (res.code === 400) {
            this.$message.error(res.message)
            } else if (res.code === 200) {
             this.$message({
                type: 'success',
                message: res.message
             })
          if (res.data) {
          localStorage.setItem("token", res.data);
        }
            }
      });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    toRegister() {
      this.form = {}
      this.logined = false;
      this.loginedCss = false;
    },
    toLogin() {
         this.forms = {}
      this.logined = true;
      this.loginedCss = true;
    },
    registerFn(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("/user/save", this.form).then((res) => {
            if (res.code === 400) {
            this.$message.error(res.message)
            } else if (res.code === 200) {
             this.$message({
                type: 'success',
                message: res.message
             })
             this.$set(this.forms, 'userName', this.form.userName)
             this.$set(this.forms, 'password', this.form.password)
            this.logined = true;
             this.loginedCss = true;
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
@import "./index.scss";
</style>
